<?php
/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
class CategoriesComponent
{
	function CategoriesComponent()
	{
	}

	function action(&$params, $action)
	{
		return $this->query($params);
	}

	function query(&$params) 
	{
		$handle = get_class($this);
		$language_id = (int)$params['languageId'];
		$category_id = (int)$params['categoryId'];
		$manufacturer_id = (int)$params['manufacturerId'];
		$isAdministrator = (boolean)$params['isAdministrator'];

		$cache_id = $language_id . '_' . $category_id . '_' . $manufacturer_id;
		if ($isAdministrator) $cache_id .= '_true';
		else $cache_id .= '_false';

		$json = read_cache($cache_id, $handle);
		if ($json)
		{
			if ($json == 'null') return null;
			else return json_decode($json, true);
		}

		$sqlQuery = 'SELECT c.categories_id, cd.categories_name, c.categories_image, c.parent_id, count(p.products_id) as products_count ' .
		'from ' . TABLE_CATEGORIES_DESCRIPTION . ' cd, ' . TABLE_CATEGORIES . ' c ' .
		'left join (' . TABLE_PRODUCTS_TO_CATEGORIES . ' pc, ' . TABLE_PRODUCTS . ' p) ';

		if ($isAdministrator) $sqlQuery .= 'on (c.categories_id = pc.categories_id and pc.products_id = p.products_id';
		else $sqlQuery .= 'on (c.categories_id = pc.categories_id and pc.products_id = p.products_id and p.products_status=1';
				
	 	$hideEmpty = false;
		if ($manufacturer_id > 0) {
			$hideEmpty = true;
			$sqlQuery .= ' and p.manufacturers_id=' . $manufacturer_id;
		}

		$sqlQuery .= ') ';

		$sqlQuery .= 'where c.categories_id = cd.categories_id and cd.language_id=' . $language_id . ' ';

		$sqlQuery .= 'group by c.categories_id ' .
		'order by c.parent_id, c.sort_order, cd.categories_name';

		$dataReturned = mysql_query($sqlQuery) or die(mysql_error());

		$width_max = (int)SUBCATEGORY_IMAGE_WIDTH;
		$height_max = (int)SUBCATEGORY_IMAGE_HEIGHT;
		
		$value{$handle . 'MetaData'}{'store_name'}= STORE_NAME;
		$value{$handle . 'MetaData'}{'show_counts'}=(boolean)SHOW_COUNTS;
		$value{$handle . 'MetaData'}{'image_width_max'}= $width_max;
		$value{$handle . 'MetaData'}{'image_height_max'}= $height_max;
		$value{$handle . 'MetaData'}{'small_image_width_max'}= (int)SMALL_IMAGE_WIDTH;
		$value{$handle . 'MetaData'}{'small_image_height_max'}= (int)SMALL_IMAGE_HEIGHT;
		$value{$handle . 'MetaData'}{'categories_per_row_max'}= (int)MAX_DISPLAY_CATEGORIES_PER_ROW;
		$value{$handle . 'MetaData'}{'hide_empty_categories'}= (boolean)$hideEmpty;
		$value{$handle . 'MetaData'}{'current_id'}= (int)$category_id;

		$i = 0;
		$j = 0;
		while($row = mysql_fetch_array($dataReturned))
		{
			$current_category_id = (int)$row['categories_id'];
			$products_count = (int)$row['products_count'];
			$value{$handle}{$i}{'id'}= $current_category_id;
			$value{$handle}{$i}{'name'}= $row['categories_name'];
			$imagename = $row['categories_image'];
			list($cat_imagename,$cat_imagewidth, $cat_imageheight) = get_rightsize_image($imagename,$width_max,$height_max);
			$value{$handle}{$i}{'image'}= $cat_imagename;
			$value{$handle}{$i}{'image_width'}= $cat_imagewidth;
			$value{$handle}{$i}{'image_height'}= $cat_imageheight;
			$value{$handle}{$i}{'parent_id'}= (int)$row['parent_id'];
			$value{$handle}{$i}{'count'}= $products_count;

			//Used for figuring out previous next.
			if ($products_count > 0)
			{
                        	$sqlQueryInner = 'select p.products_id, pd.products_name from ' . TABLE_PRODUCTS . ' p, ' . TABLE_PRODUCTS_TO_CATEGORIES . ' ptc, ' . TABLE_PRODUCTS_DESCRIPTION . ' pd ';

				if ($isAdministrator) $sqlQueryInner .= 'where p.products_id = ptc.products_id and ptc.categories_id=' . $current_category_id . ' ';
				else $sqlQueryInner .= 'where p.products_status=1 and p.products_id = ptc.products_id and ptc.categories_id=' . $current_category_id . ' ';

				$sqlQueryInner .= 'and p.products_id = pd.products_id ';

				if ($manufacturer_id > 0) {
					$sqlQueryInner .= 'and p.manufacturers_id = \'' . $manufacturer_id . '\' ';
				}

				$sqlQueryInner .= 'group by p.products_id ' .
				'order by pd.products_name ASC';

				$dataReturnedInner = mysql_query($sqlQueryInner) or die(mysql_error());

				$j=0;
				while($rowInner = mysql_fetch_array($dataReturnedInner))
				{
					$value{$handle}{$i}{'products'}{$j}{'id'}= (int)$rowInner['products_id'];
					$value{$handle}{$i}{'products'}{$j}{'name'}= $rowInner['products_name'];
					$j++;
				}
			}

			$i++;
		}

		if ($value != null) $json = json_encode($value);
		write_cache($json, $cache_id, $handle);

		return $value;
	}
}
?>
